package org.jeecg.modules.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import me.zhyd.oauth.utils.StringUtils;
import org.jeecg.common.system.base.service.impl.JeecgServiceImpl;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.mapper.SysMessageMapper;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.workflow.entity.WorkFlowBase;
import org.jeecg.modules.workflow.entity.WorkFlowBaseAssign;
import org.jeecg.modules.workflow.mapper.WorkFlowBaseAssignMapper;
import org.jeecg.modules.workflow.mapper.WorkFlowBaseMapper;
import org.jeecg.modules.workflow.service.IWorkFlowBaseAssignService;
import org.jeecg.modules.workflow.service.IWorkFlowBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 消息
 * @Author: jeecg-boot
 * @Date:  2019-04-09
 * @Version: V1.0
 */
@Service
public class WorkFlowBaseServiceImpl extends JeecgServiceImpl<WorkFlowBaseMapper, WorkFlowBase> implements IWorkFlowBaseService {

    @Autowired
    WorkFlowBaseMapper workFlowBaseMapper;

    @Autowired
    IWorkFlowBaseAssignService workFlowBaseAssignService;
    @Override
    public IPage<WorkFlowBase> queryWorkFlowBaseList(IPage<WorkFlowBase> page, WorkFlowBase workFlowBase) {
        return workFlowBaseMapper.queryWorkFlowBaseList(page,workFlowBase);
    }

    @Override
    public void addWorkFlowBase(WorkFlowBase workFlowBase) {
        String workFlowId = "";
        if(StringUtils.isEmpty(workFlowBase.getWorkflowId())){
            workFlowId =  UUIDGenerator.generate();
            //新增到基础信息表
            workFlowBase.setWorkflowId(workFlowId);
            workFlowBaseMapper.insert(workFlowBase);
        }else{
            workFlowId =workFlowBase.getWorkflowId() ;
            UpdateWrapper<WorkFlowBase> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("workflow_name",workFlowBase.getWorkflowName());
            updateWrapper.set("create_by",workFlowBase.getCreateBy());
            updateWrapper.eq("workflow_id",workFlowBase.getWorkflowId());
            this.update(updateWrapper);
        }

        //封装
        List<String> assignIdList = workFlowBase.getAssignList();
        List<WorkFlowBaseAssign> assignList =  new ArrayList<>();

        for(String assignId : assignIdList){
            WorkFlowBaseAssign workFlowBaseAssign = new WorkFlowBaseAssign();
            workFlowBaseAssign.setWorkflowId(workFlowId);
            workFlowBaseAssign.setAssignDept(assignId);
            workFlowBaseAssign.setCreateBy(workFlowBase.getCreateBy());
            assignList.add(workFlowBaseAssign);
        }
        QueryWrapper<WorkFlowBaseAssign> removeWrapper = new QueryWrapper();
        removeWrapper.eq("workflow_id",workFlowId);
        workFlowBaseAssignService.remove(removeWrapper);
        workFlowBaseAssignService.saveBatch(assignList);
    }
}
